package com.hrg.rds.biz.comm;

import cn.hutool.json.JSONUtil;
import com.hrg.rds.biz.config.RdsConfiguration;
import com.hrg.seer.sdk.rbk.RbkResult;
import com.hrg.seer.sdk.rbk.RbkResultKind;
import com.hrg.seer.sdk.rbk.api.JackOperationType;
import com.hrg.seer.sdk.rbk.api.RollerInverseType;
import com.hrg.seer.sdk.rbk.api.MiscApiEnum;
import com.hrg.seer.sdk.rbk.api.RollerOperationType;
import com.hrg.seer.sdk.rbk.msg.BaseRes;
import com.hrg.seer.sdk.rbk.msg.misc.RollerInverseReq;
import com.hrg.seer.sdk.rbk.msg.misc.SetChargeRelayReq;
import com.hrg.seer.sdk.rbk.msg.misc.SetJackHeightReq;
import com.hrg.seer.sdk.rbk.msg.nav.DIO;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class MiscService {
	// 这里的接口最好是给出接口请求是否正常，返回结果是否成功由调用方判断
	private static RbkResult ROBOT_OTHER_SETCHARGINGRELAY_REQ(String ip, boolean status) {
		int apiNo = MiscApiEnum.ROBOT_OTHER_SETCHARGINGRELAY_REQ.getApiNo();
		SetChargeRelayReq req = SetChargeRelayReq.builder().status(status).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean openChargeRelay(String ip) {
		RbkResult rbkResult = ROBOT_OTHER_SETCHARGINGRELAY_REQ(ip, true);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("打开充电继电器成功 ip = {}", ip);
				return true;
			}
			log.error("打开充电继电器失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("打开充电继电器失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	public static boolean closeChargeRelay(String ip) {
		RbkResult rbkResult = ROBOT_OTHER_SETCHARGINGRELAY_REQ(ip, false);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("关闭充电继电器成功 ip = {}", ip);
				return true;
			}
			log.error("关闭充电继电器失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("关闭充电继电器失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}


	/**
	 * 顶升车
	 */
	private static RbkResult ROBOT_OTHER_JACK_OPERATION_REQ(String ip, JackOperationType jackOperationType) {
		int apiNo = jackOperationType.getApiEnum().getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean jackOperation(String ip, JackOperationType jackOperationType) {
		RbkResult rbkResult = ROBOT_OTHER_JACK_OPERATION_REQ(ip, jackOperationType);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("{},发送成功 ip = {}", jackOperationType.getChin(), ip);
				return true;
			}
			log.error("{},发送失败 ret_code = {}, err_msg = {}，ip = {}", jackOperationType.getChin(), res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("{},发送失败 kind = {}，ip = {}", jackOperationType.getChin(), rbkResult.getKind().name(), ip);
		return false;
	}

	private static RbkResult ROBOT_OTHER_JACK_SET_HEIGHT_REQ(String ip, double height) {
		int apiNo = MiscApiEnum.ROBOT_OTHER_JACK_SET_HEIGHT_REQ.getApiNo();
		SetJackHeightReq req = SetJackHeightReq.builder().height(height).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean setJackHeight(String ip, double height) {
		RbkResult rbkResult = ROBOT_OTHER_JACK_SET_HEIGHT_REQ(ip, height);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("顶升定高,发送成功 ip = {}", ip);
				return true;
			}
			log.error("顶升定高,发送失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("顶升定高,发送失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}

	/**
	 * 辊筒车
	 * 机器人运动过程中不接受辊筒运动指令。
	 */
	private static RbkResult ROBOT_OTHER_ROLLER_OPERATION_REQ(String ip, RollerOperationType rollerOperationType) {
		int apiNo =rollerOperationType.getApiEnum().getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, null);
	}

	public static boolean rollerOperation(String ip, RollerOperationType rollerOperationType) {
		RbkResult rbkResult = ROBOT_OTHER_ROLLER_OPERATION_REQ(ip, rollerOperationType);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("{},发送成功 ip = {}", rollerOperationType.getChin(), ip);
				return true;
			}
			log.error("{},发送失败 ret_code = {}, err_msg = {}，ip = {}", rollerOperationType.getChin(), res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("{},发送失败 kind = {}，ip = {}", rollerOperationType.getChin(), rbkResult.getKind().name(), ip);
		return false;
	}

	private static RbkResult ROBOT_OTHER_ROLLER_INVERSE_REQ(String ip, RollerInverseType rollerInverseType, boolean inverse) {
		int apiNo = rollerInverseType.getApiEnum().getApiNo();
		RollerInverseReq req = RollerInverseReq.builder().inverse(inverse).build();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(req));
	}

	public static boolean rollerInverse(String ip, RollerInverseType rollerInverseType, boolean inverse) {
		RbkResult rbkResult = ROBOT_OTHER_ROLLER_INVERSE_REQ(ip, rollerInverseType, inverse);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("{} = {},发送成功 ip = {}", rollerInverseType.getChin(), inverse, ip);
				return true;
			}
			log.error("{} = {},发送失败 ret_code = {}, err_msg = {}，ip = {}", rollerInverseType.getChin(), inverse, res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("{} = {},发送失败 kind = {}，ip = {}", rbkResult.getKind().name(), rollerInverseType.getChin(), inverse, ip);
		return false;
	}

	private static RbkResult ROBOT_OTHER_SETDOS_REQ(String ip, List<DIO> dios) {
		int apiNo = MiscApiEnum.ROBOT_OTHER_SETDOS_REQ.getApiNo();
		return RdsConfiguration.AgvIpClientMap.get(ip).request(apiNo, JSONUtil.toJsonStr(dios));
	}

	public static boolean setDOS(String ip, List<DIO> dios) {
		RbkResult rbkResult = ROBOT_OTHER_SETDOS_REQ(ip, dios);
		if (rbkResult.getKind().equals(RbkResultKind.Ok)) {
			BaseRes res = JSONUtil.toBean(rbkResult.getResStr(), BaseRes.class);
			if (res.getRet_code() == 0) {
				log.info("批量设置 DO,发送成功 ip = {}", ip);
				return true;
			}
			log.error("批量设置 DO,发送失败 ret_code = {}, err_msg = {}，ip = {}", res.getRet_code(), res.getErr_msg(), ip);
		}
		log.error("批量设置 DO,发送失败 kind = {}，ip = {}", rbkResult.getKind().name(), ip);
		return false;
	}
}
